package com.zt.rongda.salary.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.salary.model.SalaryPayRoll;
import com.zt.rongda.salary.service.SalaryPayRollService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/salaryPayRoll")
public class SalaryPayRollController {

    @Autowired
    private SalaryPayRollService salaryPayRollService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(salaryPayRollService.getTable(reqBean));
    }
    
    @RequestMapping(value = "/getSalaryPayRollYear")
    @ResponseBody
    
    public JSONObject getSalaryPayRollYear(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	return Response.response(salaryPayRollService.getSalaryPayRollYear());
    }
    @RequestMapping(value = "/getSalaryPayRollMonthByYear")
    @ResponseBody
    
    public JSONObject getSalaryPayRollMonthByYear(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	int salaryPayRollYear = reqBean.getInfoInt("salaryPayRollYear");
    	return Response.response(salaryPayRollService.getSalaryPayRollMonthByYear(salaryPayRollYear));
    }
    
    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryPayRollService.add(getSalaryPayRoll(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryPayRollService.update(getSalaryPayRoll(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryPayRollService.delete(reqBean.getInfo("payrollId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private SalaryPayRoll getSalaryPayRoll(ReqBean reqBean) {
        SalaryPayRoll salaryPayRoll = new SalaryPayRoll();
        salaryPayRoll.setPayrollId(reqBean.getInfo("payrollId"));
        salaryPayRoll.setPayrollYear(reqBean.getInfoInt("payrollYear"));
        salaryPayRoll.setPayrollMonth(reqBean.getInfoInt("payrollMonth"));
        salaryPayRoll.setStatus(reqBean.getInfo("status"));
        salaryPayRoll.setOperator(reqBean.getUserId());
        salaryPayRoll.setRemark(reqBean.getInfo("remark"));
        return salaryPayRoll;
    }

}